package com.wnhz.work.yz.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.util.LogUtils;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MainActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.activity.forget.ForgetPasswordActivity;
import com.wnhz.work.yz.bean.UserBean;
import com.wnhz.work.yz.constant.Constants;
import com.wnhz.work.yz.utils.BroadCastReceiverUtil;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import cn.jpush.android.api.JPushInterface;

/**
 * 作者： zxb
 * 日期： 2016/12/30
 * 描述： 新的登录页面
 */
public class LoginNewActivity extends BaseActivity implements View.OnClickListener, TextWatcher {

    private EditText login_phone_new;
    private EditText login_pwd_new;
    private ImageView iv_showPwd_new;
    private Button login_submit_new;

    private String inputPhone, inputPwd;
    private boolean isPwdShow = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_new);
        initData();
        initTitle();
        initView();
    }

    private void initTitle() {
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.btn_main_right).setVisibility(View.VISIBLE);
        findViewById(R.id.btn_main_right).setOnClickListener(this);
        TextView title_right = (TextView) findViewById(R.id.view_main_right_text);
        title_right.setVisibility(View.VISIBLE);
        title_right.setTextColor(0xff333333);
        title_right.setText("忘记密码？");
    }

    private void initData() {

    }

    private void initView() {
        login_phone_new = (EditText) findViewById(R.id.login_phone_new);
        login_pwd_new = (EditText) findViewById(R.id.login_pwd_new);
        iv_showPwd_new = (ImageView) findViewById(R.id.iv_showPwd_new);
        login_submit_new = (Button) findViewById(R.id.login_submit_new);

        iv_showPwd_new.setOnClickListener(this);
        login_submit_new.setOnClickListener(this);

        login_phone_new.addTextChangedListener(this);
        login_pwd_new.addTextChangedListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.btn_main_right:
                intent = new Intent(this, ForgetPasswordActivity.class);
                startActivity(intent);
                break;
            case R.id.iv_showPwd_new:
                isPwdShow = !isPwdShow;
                if (isPwdShow) {
                    //设置密码文本为显示的
                    iv_showPwd_new.setImageResource(R.mipmap.ic_xianshimima_li);
                    login_pwd_new.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    //设置密码文本为隐藏的
                    login_pwd_new.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    iv_showPwd_new.setImageResource(R.mipmap.ic_xianshimima_nor);
                }

                login_pwd_new.postInvalidate();
//                //切换后将EditText光标置于末尾
//                CharSequence charSequence = login_pwd_new.getText();
//                if (charSequence instanceof Spannable) {
//                    Spannable spanText = (Spannable) charSequence;
//                    Selection.setSelection(spanText, charSequence.length());
//                }

                break;
            case R.id.login_submit_new:
                inputPhone = login_phone_new.getText().toString().trim();
                inputPwd = login_pwd_new.getText().toString().trim();

                if (!TextUtils.isEmpty(inputPhone) && inputPwd.length() > 5) {
                    upload();
                } else if (TextUtils.isEmpty(inputPhone)) {
                    MyToast(getString(R.string.toast_input_phone));
                } else if (inputPwd.length() <= 5) {
                    MyToast(getString(R.string.toast_input_pwd));
                }

                break;
            default:
                break;
        }

    }

    private static final String TAG = "LoginNewActivity";

    private void upload() {
        String registrationID = JPushInterface.getRegistrationID(getBaseContext());
        Log.e("极光registrationID", registrationID);
        showDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("phone", inputPhone);
        map.put("password", inputPwd);
        map.put("chat_id", registrationID);


        XUtil.Post(Url.USER_LOGIN, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {

                super.onSuccess(result);
                Log.i(TAG, "onSuccess: LoginNewActivity= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    String token = jsonObject.optString("token");
                    String url = jsonObject.optString("url");

                    String message = jsonObject.optString("message");
                    String is_c = jsonObject.optString("is_c");
                    String area = jsonObject.optString("area");
                    String ratio = jsonObject.optString("ratio");
                    if ("1".equals(res)) {
                        MyToast(info);
                        //保存
                        SharedPreferences sd = getSharedPreferences("login", 0);
                        SharedPreferences.Editor editor = sd.edit();
                        editor.putString("phone", inputPhone);
                        editor.putString("password", inputPwd);
                        editor.commit();
                        UserBean bean = new UserBean();
                        bean.setPhone(inputPhone);
                        bean.setPassWord(inputPwd);
                        bean.setToken(token);
                        bean.setHeadUrl(url);
                        bean.setPhone(inputPhone);
                        bean.setPassWord(inputPwd);
                        bean.setArea(area);
                        bean.setIs_c(is_c);
                        bean.setRatio(ratio);

                        MyApplication.getInstance().setUserBean(bean);
                        MyApplication.getInstance().userBean = bean;
                        BroadCastReceiverUtil.sendBroadcast(LoginNewActivity.this, Constants.LOGO_SUCCESS);

                        Intent intent = new Intent(LoginNewActivity.this, MainActivity.class);
                        intent.putExtra("Pagination", 0);
                        startActivity(intent);

                    } else {
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
                super.onError(ex, isOnCallback);
            }
        });
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        String inputPhone = login_phone_new.getText().toString().trim();
        String inputPwd = login_pwd_new.getText().toString().trim();

        if (!TextUtils.isEmpty(inputPhone) && !TextUtils.isEmpty(inputPwd)) {
            login_submit_new.setTextColor(0xffffffff);
        } else {
            login_submit_new.setTextColor(0x66ffffff);
        }

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}
